package com.turbo.cloud.bpm.web.logic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.bpm.web.entity.CustomFlwGroup;
import com.turbo.cloud.bpm.web.entity.CustomFlwInfo;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwInfoDO;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwInfoQO;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwInfoVO;
import com.turbo.cloud.bpm.web.service.ICustomFlwInfoService;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.constants.CommConstants;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.utils.PageQueryUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * <p>
 * 流程信息表 逻辑处理类
 * </p>
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class FlwInfoLogic {

    final ICustomFlwInfoService customFlwInfoService;

    /**
     * 查询流程信息分页列表
     *
     * @param pageQuery 分页查询参数
     * @return Page
     */
    public PageQuery<CustomFlwInfoVO> queryFlwInfoPage(PageQuery<CustomFlwInfoQO> pageQuery) {
        CustomFlwInfoQO customFlwInfoQO = PageQueryUtil.getPageParam(pageQuery, CustomFlwInfoQO::new);

        LambdaQueryWrapper<CustomFlwInfo> flwInfoLQW = new LambdaQueryWrapper<>();

        flwInfoLQW.eq(StrUtil.isNotEmpty(customFlwInfoQO.getGroupId()), CustomFlwInfo::getGroupId, customFlwInfoQO.getGroupId());
        flwInfoLQW.eq(Objects.nonNull(customFlwInfoQO.getStateId()), CustomFlwInfo::getStateId, customFlwInfoQO.getStateId());

        // 模糊查询 组名称和组编码
        flwInfoLQW.and(StrUtil.isNotEmpty(customFlwInfoQO.getNameByLike()), i -> i.like(CustomFlwInfo::getName,
                        customFlwInfoQO.getNameByLike())
                .or()
                .like(StrUtil.isNotEmpty(customFlwInfoQO.getNameByLike()), CustomFlwInfo::getCode,
                        customFlwInfoQO.getNameByLike()));

        flwInfoLQW.orderByDesc(CustomFlwInfo::getCreatedBy);
        Page<CustomFlwInfo> flwGroupPage = customFlwInfoService.page(pageQuery.toPage(), flwInfoLQW);

        return PageQueryUtil.toPageQuery(flwGroupPage, CustomFlwInfoVO::new);
    }

    /**
     * 新增流程信息
     *
     * @param customFlwInfoDO 实体
     */
    public Boolean createFlwInfo(CustomFlwInfoDO customFlwInfoDO) {
        CustomFlwInfo customFlwInfo = new CustomFlwInfo();
        BeanUtils.copyProperties(customFlwInfoDO, customFlwInfo);

        boolean result = customFlwInfoService.save(customFlwInfo);
        if (!result) {
            throw new BusinessException("新增流程信息失败");
        }
        return true;
    }

    /**
     * 修改流程信息
     *
     * @param customFlwInfoDO 实体
     */
    public Boolean modifyFlwInfo(CustomFlwInfoDO customFlwInfoDO) {
        CustomFlwInfo umsFlwInfo = new CustomFlwInfo();
        BeanUtils.copyProperties(customFlwInfoDO, umsFlwInfo);

        boolean result = customFlwInfoService.updateById(umsFlwInfo);
        if (!result) {
            throw new BusinessException("修改流程信息失败");
        }
        return true;
    }

    /**
     * 删除流程信息
     *
     * @param id 流程信息表ID
     */
    public boolean removeFlwInfoById(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BusinessException("流程信息ID不能为空");
        }

        boolean result = customFlwInfoService.removeById(id);
        if (!result) {
            throw new BusinessException("删除流程信息失败");
        }
        return true;
    }

    /**
     * 开启、关闭流程信息有效状态
     *
     * @param id 流程信息ID
     */
    public Boolean enabledFlwInfoStateById(String id) {
        CustomFlwInfo customFlwInfo = customFlwInfoService.getById(id);
        if (Objects.isNull(customFlwInfo)) {
            throw new BusinessException("流程信息不存在");
        }

        if (CommConstants.DICT_STATE_ID_YES.equals(customFlwInfo.getStateId())) {
            customFlwInfo.setStateId(0);
        } else {
            customFlwInfo.setStateId(1);
        }

        boolean result = customFlwInfoService.updateById(customFlwInfo);
        if (!result) {
            throw new BusinessException("修改流程信息状态失败");
        }
        return true;
    }
}
